package com.example.base.net

import com.blankj.utilcode.util.SPUtils
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

/**
 * 网络请求工具类---retrofit+okhttp
 */
class RetrofitUtils {
    companion object{
        fun getRetrofit():Retrofit{
            val client = OkHttpClient.Builder()
                .readTimeout(5,TimeUnit.SECONDS)//读超时
                .writeTimeout(5,TimeUnit.SECONDS)//写超时
                .connectTimeout(5,TimeUnit.SECONDS)//连接超时
                .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))//日志拦截器
                    //自定义拦截器
                .addInterceptor {
                    val token = SPUtils.getInstance().getString("token")
                    val request = it.request().newBuilder().addHeader("Authori-zation",token).build()
                    it.proceed(request)
                }
                .build()

            val retrofit = Retrofit.Builder()
                .baseUrl(Cons.BASE_URL)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
            return retrofit

        }
    }
}